package com.atguigu.eduservice.controller.frontController;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.OrderClient;
import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.vo.CourseFrontVo;
import com.atguigu.eduservice.entity.vo.CourseWebVo;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.impl.EduChapterService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author cyt
 * @create 2020-05-16 10:36
 */
@Api(description = "前台课程")
@RestController
@RequestMapping("/eduservice/frontCourse")

public class CourseFrontController {
    @Autowired
    EduCourseService eduCourseService;
    @Autowired
    OrderClient orderClient;

    // 前端 带条件查询带分页
    @ApiOperation(value = "分页课程列表 , 条件")
    @PostMapping(value = "{page}/{limit}")
    public R pageList(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "courseQuery", value = "查询对象", required = false)
            @RequestBody(required = false) CourseFrontVo courseFront) {

        Page<EduCourse> pageParam = new Page<EduCourse>(page, limit);
        Map<String, Object> map = eduCourseService.pageListWeb(pageParam, courseFront);
        return R.ok().data(map);
    }

    // 根据课程的id 查询到课程的信息
    @GetMapping("/getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable() String courseId, HttpServletRequest request) {
        // 课程基本信息
        CourseWebVo courseWebVo = eduCourseService.getBaseInfo(courseId);
        // 所有的章节 信息
        List<ChapterVo> allinfo = eduCourseService.getAllinfo(courseId);
        // 远程调用
        boolean idBuy = orderClient.idBuy(JwtUtils.getMemberIdByJwtToken(request), courseId);

        return R.ok().data("courseWebVo", courseWebVo).data("allinfo", allinfo).data("isBuy", idBuy);
    }

}
